import { Platform } from 'react-native';

// OCR识别后的发票数据结构
interface OCRReceiptData {
  merchantName?: string;
  date?: string;
  items?: Array<{
    id: string;
    name: string;
    quantity: number;
    price: number;
    amount: number;
  }>;
  taxAmount?: number;
  totalAmount?: number;
}

// 模拟OCR服务 - 在实际应用中应该调用真实的OCR API
export const processReceiptWithOCR = async (imageUri: string): Promise<OCRReceiptData> => {
  // 在实际应用中，这里应该上传图片到OCR服务，并获取识别结果
  // 这里使用随机延迟来模拟网络请求
  await new Promise(resolve => setTimeout(resolve, 1000 + Math.random() * 1000));
  
  // 模拟OCR识别结果
  const mockItems = [
    {
      id: '1',
      name: '办公用纸A4',
      quantity: 2,
      price: 45.5,
      amount: 91.0
    },
    {
      id: '2',
      name: '签字笔',
      quantity: 10,
      price: 5.5,
      amount: 55.0
    },
    {
      id: '3',
      name: '文件夹',
      quantity: 5,
      price: 12.8,
      amount: 64.0
    }
  ];
  
  const totalAmount = mockItems.reduce((sum, item) => sum + item.amount, 0);
  const taxAmount = totalAmount * 0.13; // 假设13%的税率
  
  return {
    merchantName: '文具大世界',
    date: new Date().toISOString().split('T')[0], // 今天的日期
    items: mockItems,
    taxAmount: Math.round(taxAmount * 100) / 100,
    totalAmount: Math.round((totalAmount + taxAmount) * 100) / 100
  };
};

// 处理上传发票图片
export const uploadReceiptImage = async (uri: string): Promise<string> => {
  // 在实际应用中，这里应该上传图片到服务器或云存储
  // 现在简单地返回原始URI
  
  // 模拟上传延迟
  await new Promise(resolve => setTimeout(resolve, 1000));
  
  return uri;
};

// 获取发票文件名
export const getReceiptFileName = (uri: string): string => {
  const fileName = uri.split('/').pop() || `receipt_${Date.now()}.jpg`;
  return fileName;
};

// 检查是否有必要的权限
export const checkPermissions = async () => {
  // 在实际应用中应该检查相机和存储权限
  return true;
}; 